package autoDraft;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Draft {

	short Round;
	Queue<String> draftOrder;
	//ArrayList<Team> draftOrder;
	ArrayList<Player> availPlayers;
	ArrayList<Player> draftedPlayers;
	ArrayList<Team> teams;
	int numOfPlayers;
	
	public Draft()
	{
		this.Round = 1;
		this.draftOrder= new LinkedList<String>();
		this.availPlayers = new ArrayList<Player>();
		this.teams = new ArrayList<Team>();
		this.draftedPlayers = new ArrayList<Player>();
		
	}
	private Draft(short Round, ArrayList<Team> draftOrder,ArrayList<Player> availPlayers){
		
		Round = 1;
		
	}
	
	
	
	void genPlayers(File file)
	{
		String[] strTemp;
		String everything = "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
			        String line = "";
	        line = br.readLine();
	        while (line != null) {
	            strTemp = line.split("\\s+");
	            Player player = new Player();
	            updatePlayer(strTemp, player);
	            this.availPlayers.add(player);
	            line = br.readLine();
	        }
	        } catch(Exception e) {
			System.out.println(e);
		}
		this.numOfPlayers = availPlayers.size() - 1;
	}
	
	
	private void updatePlayer(String[] strTemp, Player player) {
		player.Rating = Integer.parseInt(strTemp[0]);
		player.fName = strTemp[1];
		player.lName = strTemp[2];
		player.position = strTemp[3]; 
		player.posRank = Integer.parseInt(strTemp[4]);
		player.school = strTemp[5];
		player.classYear = strTemp[6];
		player.height = strTemp[7];
		player.weight = Integer.parseInt(strTemp[8]);
		player.suggestedRound = Integer.parseInt(strTemp[9]);
		//System.out.println(player.lName + " , " + player.fName);
	}
	
	
	
	boolean genDraftOrder(File file)
	{
		String[] strTemp;
		try {
			BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
			StringBuilder sb = new StringBuilder();
	        String line = "";
	        line = br.readLine();
	        while (line != null) {
	            strTemp = line.split("\\s+");
	                        
	            line = br.readLine();
	            
	            this.draftOrder.add(strTemp[2]); //adding to queue
	           // System.out.println(strTemp[2]);
	                 
	        }
	       
		} catch(Exception e) {
			System.out.println(e);
		}
		return false;
	}
	
	void genTeams(File file)
	{
		String[] strTemp;
		try {
			BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
	        String line = "";
	        line = br.readLine();
	        while (line != null) {
	            strTemp = line.split("\\s+");
	            Team team = new Team();
	            team.mascot = strTemp[0];
	            for (int i = 1; i < strTemp.length; i++)
	            {
	            	team.needs.add(strTemp[i]);
	            }
	            
	            line = br.readLine();
	          //  System.out.println(team.mascot);
	            this.teams.add(team);
	        }
	       
		} catch(Exception e) {
			System.out.println(e);
		}
	}

	void simDraftBPA()
	{
		//get draft order
		//for each pick, get team
		//for team, draft player by ranking
		
		for(int i= 0; i < availPlayers.size(); i++)
		{
			String aMascot = this.draftOrder.remove();
			Player draftee = this.availPlayers.remove(0);
			this.draftedPlayers.add(draftee);
			for(int j = 0; j < teams.size(); j++)
			{
				if(teams.get(j).mascot.equals(aMascot))
				{
					System.out.println("The " + teams.get(j).mascot + " draft: " + draftee.fName + " " + draftee.lName);
					
				}
			}
			
		}
		
	}
	
	void simDraftTeamNeedsBPA()
	{
		//get draft order
		//for each pick, get team
		//for team, draft player by need
		//for each need find player with highest ranking avail of each need
		//draft highest of players found
				
		for(int i= 0; i < this.numOfPlayers; i++)
		{
			if( i > 0 && i % 32 == 0)
				Round++;
			String aMascot = this.draftOrder.remove();
			for(int j = 0; j < teams.size(); j++)
			{
				if(teams.get(j).mascot.equals(aMascot)) //teams
				{
					System.out.println("The " + aMascot + " are now on the clock...");
					//Player draftee = new Player();
					ArrayList<Player> potentialDraftees = new ArrayList<Player>();
					String needs = "";
					for( int k = 0; k < teams.get(j).needs.size(); k++) // needs
					{
						 String pos = teams.get(j).needs.get(k);
						 for( int l = 0; l < availPlayers.size(); l++) // players
						 {
							if(availPlayers.get(l).position.equals(pos))
							{
								potentialDraftees.add(availPlayers.get(l));
								break;
							}
						 }
						 needs = needs +  " " + teams.get(j).needs.get(k);
					}
					System.out.println("Their needs are : " + needs);
					Player draftee = new Player(); 
					if(potentialDraftees.size() == 0)
					{
						draftee = availPlayers.remove(0);
					}
					else 
					{
						draftee = potentialDraftees.get(0);
						for(int index = 1; index < potentialDraftees.size(); index++)
						{
							if( potentialDraftees.get(index).Rating < draftee.Rating)
							{
								draftee = potentialDraftees.get(index);
							}
						}
						//remove player from avail list
											
						availPlayers.remove(draftee);
						teams.get(j).needs.remove(draftee.position);
					}
					
					
					System.out.println("With pick number "+ String.valueOf(i + 1) + " in Round " + this.Round + " of the 2014 NFL Draft");
					System.out.println("The " + teams.get(j).mascot + " draft " + draftee.position + " " + draftee.fName + " " + draftee.lName + " of " + draftee.school + "\n\n");
					
							
				}
			}
					
		}
	}
	
	
	
}